#encoding=utf8
'''
Created on 2012-9-21

@author: Administrator
'''
from Processor import Processor
from models.MessagesModel import MessagesModel
from models.User import User
from models.UserModel import *
from pub import Func, UserDict
from pub.Config import Config

class PISClass(Processor):
    '''
    指令：PIS(push infos)
    备注：发送消息
    入口格式：cmd #cmdID#userID#[infoType] #[startPosition] #[count]
    出口格式：
    '''
    def __init__(self,):
        Processor.__init__(self,)
        self.__outdata=None
        self.__field = {}
    def dataValidtor(self,cmdContent):
        print len(cmdContent);
        if (len(cmdContent)==4):
            
            self.__field["userId"] = Func.strToInt(cmdContent[0], 0)
            self.__field["infoType"] =Func.strToInt( cmdContent[1],-1);
            self.__field["startPosition"] = Func.strToInt( cmdContent[2],0)
            self.__field["count"] = Func.strToInt( cmdContent[3],100);
            return True;
        return False;
    def getMessage(self):
        if self.__field["userId"]>0:
            oMsg = MessagesModel()
            dMsgList = oMsg.getMessageList(0, self.__field["userId"], 0, None, None)
        data=[]
        if dMsgList!=None:
            for msg in dMsgList:
    #            tmp[msg["Id"]]["title"]=msg["title"]
    #            tmp[msg["Id"]]["infoType"]=msg["Msgtype"]
                try:
                    if msg["Msgtype"]==2:#如果是好友邀请消息，则分割Content ^
                        
                            sTmp = msg["Content"].split("^")
                            print sTmp
                            msg["requestFlag"] = sTmp[0]
                            msg["keywords"] = sTmp[1]
                            msg["roomTitle"] = sTmp[2]
                            msg["infos"] = sTmp[3]
    
                    elif msg["Msgtype"]==1:
                        msg["requestFlag"] = 0
                        msg["keywords"] = 0
                        msg["roomTitle"] = 0
                        msg["infos"] = msg["Content"]
                    if msg["MsgSender"]==0:
                        msg["UserNickName"]="系统用户"
                    data.append(msg["Titile"])
                    data.append(msg["Msgtype"])
                    data.append(msg["Created"])
                    data.append(msg["MsgSender"])
                    data.append(msg["UserNickName"])
                    data.append(msg["requestFlag"])
                    data.append(msg["keywords"])
                    data.append(msg["roomTitle"])
                    data.append(msg["infos"])
                except :
                        continue        
        return data
    def outData(self):
        '''
        输出执行结果集
        '''
        data = self.getMessage()
        command = self.setPackage(data)
        self._sockObj.sendLine(command)    
        return Config.I_SYS_ONE;